package org.steve.betterattack.Abilities;

import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.BlockVector;
import org.steve.betterattack.BetterAttack;
public class ArrowRain implements Ability{
    public String name="";
    public int level=0;
    int coolDownTick;
    BetterAttack plugin;
    boolean coolDown=false;


    public ArrowRain(BetterAttack plugin, int level, int CDTick, String name){
        coolDownTick= CDTick;
        this.level=level;
        this.plugin=plugin;
        this.name=name;
    }
    public ArrowRain(BetterAttack plugin, ConfigurationSection config){
        this.plugin=plugin;
        name=config.getName();
        level=config.getInt("level");
        coolDownTick=config.getInt("cooldown_tick");
    }
    @Override
    public boolean execute(Entity entity, Player sender) {
        if(coolDown)return false;
        Location location=entity.getLocation();
        for (int i=0;i<level;i++){
            Location spawnLocation=entity.getLocation();
            spawnLocation.add(plugin.random.nextDouble()*6-3,10,plugin.random.nextDouble()*2);
            spawnLocation.getWorld().spawnArrow(spawnLocation,new BlockVector(0,-1,0),0.3f,10);
        }

        return true;
    }

    @Override
    public int getLevel() {
        return level;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setCoolDown(boolean cd) {
        if (cd) coolDown= true;
        else new BukkitRunnable() {
            @Override
            public void run() {
                coolDown=false;
            }
        }.runTaskLater(plugin,coolDownTick);
    }
}
